package com.siwei.mes.sync.erp;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.bus.SyncGroup;
import com.siwei.mes.mapper.materials.SupplierCompanyMaterialsMapper;
import com.siwei.mes.sync.erp.entity.SyncProduct;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 根据物料名称获取所有厂家列表
 *
 * @Description: 根据物料名称获取所有厂家列表
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/8/19 20:30
 */
@Api(value = "ERP-根据物料名称获取所有厂家列表")
@RestController
@RequestMapping("erp/product")
public class SyncProductController {
    private static final Logger log = LoggerFactory.getLogger("erp");
    @Value("${erp-token}")
    private String ERP_TOKEN;
    @Resource
    private SupplierCompanyMaterialsMapper supplierCompanyMaterialsMapper;

    /**
     * 校验token
     *
     * @param request
     */
    public void checkToken(HttpServletRequest request) {
        String token = request.getHeader(BaseParams.Authorization);
        assertTrue(StringUtils.isBlank(token), "授权token为空");
        assertTrue(!token.equals(ERP_TOKEN), "token不正确");
    }

    @AnonymousAccess
    @ApiOperation(value = "根据物料名称获取所有厂家列表")
    @PostMapping("/getAll")
    public Json saveOrUpdate(@RequestBody @Validated(SyncGroup.class) SyncProduct syncInfo, HttpServletRequest request) {
        log.info("ERP根据物料名称获取所有厂家列表，入参：{}", JSON.toJSONString(syncInfo));
        this.checkToken(request);

        List<String> list = new ArrayList<>();

        //String category = syncInfo.getCategory();
        //MaterialsEnum materialsEnum = MaterialsEnum.selectByErpType(category);
        //if (materialsEnum != null) {
        list = supplierCompanyMaterialsMapper.getManufacturerList(null, syncInfo.getName(), syncInfo.getSpec(), syncInfo.getSupplierName());
        //}
        return Json.succ().data(ApiResult.addList(list));
    }
}
